import time
import requests 
import json 

class ChanAPI:
    '''Loading and handling the storage of Thread objects''' 

    def __init__(self, board):
        self.__board = board
        self.__delay = 1 # required by api
    
    def __get(self, no=None, requestType=None):
        ''' Return a dict of the JSON structure'''

        if no and requestType=="thread":
            fchanUrl = f"https://a.4cdn.org/{self.__board}/thread/{no}.json"
            return json.loads(requests.get(fchanUrl).content)

        elif requestType:
            fchanUrl = f"http://a.4cdn.org/{self.__board}/{requestType}.json"
            return json.loads(requests.get(fchanUrl).content)

        time.sleep( self.__delay )

    def getThread(self,no):
        ''' Return a dict of the JSON structure'''
        return self.__get(no,"thread")

    def getCatalog(self):
        ''' Return a dict of the JSON structure'''
        return self.__get(requestType="catalog")
    
    def getArchive(self):
        ''' Return a dict of the JSON structure'''
        return self.__get(requestType="archive")